
# generate appendix D3, table D3

library(rio) # to load data
library(tidyverse) # to reshape data
library(lmtest) # to cluster standard errors
library(sandwich) # to cluster standard errors
library(plm) # to run panel regressions
library(texreg) # to generate tables

# set your working directory
setwd("~/replication_files")

annual_data <- import("data/annual_data.csv") 

# table D3

# model 1
plm1 <- plm(log_debt_total_delta ~
            resource_rents_perc_delta +
            log_oil_production_delta + 
            terms_of_trade_delta +
            discovery_lag +
            minister_is_econ_technocrat_usa +
            minister_turnover_5yrs +
            debt_crisis_experience +
            election_year + left_exec +
            fiscal_council + 
            polcon + 
            imf_agreement +
            fiscal_balance_lag + 
            tax_perc_gdp_cepal_lag +
            log_core_inflation_lag +
            gdp_pc_cepal_lag +
            gdp_growth_cepal_lag +
            ka_open_lag +
            log_reserves_lag +
            treasury_rate_lag,
            data = annual_data, model = "within", index = c("iso3c","year"))

plm1_r <- plm1 %>%
  coeftest(vcovHC(plm1, type = 'HC0', cluster = 'group'))

# model 2
plm2 <- plm(log_debt_multilateral_delta ~
            resource_rents_perc_delta +
            log_oil_production_delta + 
            terms_of_trade_delta +
            discovery_lag +
            minister_is_econ_technocrat_usa +
            minister_turnover_5yrs +
            debt_crisis_experience +
            election_year + left_exec +
            fiscal_council + 
            polcon + 
            imf_agreement +
            fiscal_balance_lag + 
            tax_perc_gdp_cepal_lag +
            log_core_inflation_lag +
            gdp_pc_cepal_lag +
            gdp_growth_cepal_lag +
            ka_open_lag +
            log_reserves_lag +
            treasury_rate_lag,
            data = annual_data, model = "within", index = c("iso3c","year"))

plm2_r <- plm2 %>%
  coeftest(vcovHC(plm2, type = 'HC0', cluster = 'group'))

# model 3
plm3 <- plm(log_debt_bilateral_delta ~
            resource_rents_perc_delta +
            log_oil_production_delta + 
            terms_of_trade_delta +
            discovery_lag +
            minister_is_econ_technocrat_usa +
            minister_turnover_5yrs +
            debt_crisis_experience +
            election_year + left_exec +
            fiscal_council + 
            polcon + 
            imf_agreement +
            fiscal_balance_lag + 
            tax_perc_gdp_cepal_lag +
            log_core_inflation_lag +
            gdp_pc_cepal_lag +
            gdp_growth_cepal_lag +
            ka_open_lag +
            log_reserves_lag +
            treasury_rate_lag,
            data = annual_data, model = "within", index = c("iso3c","year"))

plm3_r <- plm3 %>%
  coeftest(vcovHC(plm3, type = 'HC0', cluster = 'group'))

# model 4
plm4 <- plm(log_debt_commercial_banks_delta ~
            resource_rents_perc_delta +
            log_oil_production_delta + 
            terms_of_trade_delta +
            discovery_lag +
            minister_is_econ_technocrat_usa +
            minister_turnover_5yrs +
            debt_crisis_experience +
            election_year + left_exec +
            fiscal_council + 
            polcon + 
            imf_agreement +
            fiscal_balance_lag + 
            tax_perc_gdp_cepal_lag +
            log_core_inflation_lag +
            gdp_pc_cepal_lag +
            gdp_growth_cepal_lag +
            ka_open_lag +
            log_reserves_lag +
            treasury_rate_lag,
            data = annual_data, model = "within", index = c("iso3c","year"))

plm4_r <- plm4 %>%
  coeftest(vcovHC(plm4, type = 'HC0', cluster = 'group'))

# model 5
plm5 <- plm(log_debt_bonds_delta ~
            resource_rents_perc_delta +
            log_oil_production_delta + 
            terms_of_trade_delta +
            discovery_lag +
            minister_is_econ_technocrat_usa +
            minister_turnover_5yrs +
            debt_crisis_experience +
            election_year + left_exec +
            fiscal_council + 
            polcon + 
            imf_agreement +
            fiscal_balance_lag + 
            tax_perc_gdp_cepal_lag +
            log_core_inflation_lag +
            gdp_pc_cepal_lag +
            gdp_growth_cepal_lag +
            ka_open_lag +
            log_reserves_lag +
            treasury_rate_lag,
            data = annual_data, model = "within", index = c("iso3c","year"))

plm5_r <- plm5 %>%
  coeftest(vcovHC(plm5, type = 'HC0', cluster = 'group'))

# generate table D3
texreg(l = list(plm1_r,plm2_r,plm3_r,plm4_r,plm5_r), beside = TRUE, stars = c(0.01, 0.05, 0.1),
       custom.header = list("Dependent Variable:" = 1:5),
       custom.model.names = c("Ln Debt, Total $_Delta$",
                          "Ln Debt, Multilateral $_Delta$",
                          "Ln Debt, Bilateral $_Delta$",
                          "Ln Debt, Commercial Banks $_Delta$",
                          "Ln Debt, Bonds $_Delta$"),
       custom.coef.map = list("resource_rents_perc_delta" = "Resource Rents, % of GDP $_{Delta}$",
                          "log_oil_production_delta" = "Ln Oil and Gas Production $_{Delta}$",
                          "terms_of_trade_delta" = "Commodity Price Index $_{Delta}$",
                          "discovery_lag" = "Field Discovery $_{t-1}$",
                          "minister_is_econ_technocrat_usa" = "Mainstream Minister = 1",
                          "minister_turnover_5yrs" = "Minister Turnover (5 Years)",
                          "debt_crisis_experience" = "Debt Crisis Experience = 1",
                          "election_month" = "Election Month = 1",
                          "left_exec" = "Left Executive = 1",
                          "cb_independence" = "Central Bank Independence",
                          "fiscal_council" = "Fiscal Council = 1",
                          "polcon" = "Political Constraints",
                          "imf_agreement" = "IMF Agreement = 1",
                          "fiscal_balance_lag" = "Fiscal Balance, % of GDP $_{t-1}$",
                          "tax_perc_gdp_cepal_lag" = "Tax Revenue, % of GDP $_{t-1}$",
                          "log_core_inflation_lag" = "Ln Core Inflation $_{t-1}$",
                          "gdp_pc_cepal_lag" = "GDP Per Capita $_{t-1}$",
                          "gdp_growth_cepal_lag" = "GDP Growth, % $_{t-1}$",
                          "ka_open_lag" = "Capital Openness $_{t-1}$",
                          "log_reserves_lag" = "Ln International Reserves $_{t-1}$",
                          "treasury_rate_lag" = "US Treasury Rate, % $_{t-1}$"),
       caption = "The Effect of Natural Resources on Sovereign Borrowing: Debt Stock by Type of Creditor",
       fontsize = "footnotesize", digits = 3)

# retrieve fit stats
texreg(l = list(plm1,plm2,plm3,plm4,plm5), beside = TRUE, stars = c(0.01, 0.05, 0.1))








